package cakeorcookiexample;

import java.util.logging.Level;
import controlisolation.lifecyclefsm.LifeCycleFSMController;
import controlisolation.lifecyclefsm.NoWork;
import controlisolation.lifecyclefsm.stateexecutor.StateWork;

public class CakeorCookieFSMController extends LifeCycleFSMController {
	private NoWork _noWork=new NoWork();

	@Override
	public void controlCycle() throws Exception {
		StateWork work;
		work=_lfsm.getCurrentStateWork();
		if (work==null) {
			if (!_lfsm.isInitial(_lfsm.getCurrentState())){
				_logger.log(Level.INFO,"No work defined for state: using default!");
			}
		work=_noWork;
		}
		if (_lfsm.getCurrentState().getId().equals("Initial")){
		/* Control Code for state Initial */
		/* Possible transitions to take at this point are: 
		start will take to Init
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_Initial(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_Initial(work);
		}
		return; } 
		/* State Control Code Ended */

		if (_lfsm.getCurrentState().getId().equals("Gimme_C")){
		/* Control Code for state Gimme_C */
		/* Possible transitions to take at this point are: 
		evt_C will take to Gimme_A_or_O
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_Gimme_C(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_Gimme_C(work);
		}
		return; } 
		/* State Control Code Ended */

		if (_lfsm.getCurrentState().getId().equals("Gimme_A_or_O")){
		/* Control Code for state Gimme_A_or_O */
		/* Possible transitions to take at this point are: 
		evt_A will take to Gimme_K
		evt_O will take to Gimme_O
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_Gimme_A_or_O(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_Gimme_A_or_O(work);
		}
		return; } 
		/* State Control Code Ended */

		if (_lfsm.getCurrentState().getId().equals("Gimme_K")){
		/* Control Code for state Gimme_K */
		/* Possible transitions to take at this point are: 
		evt_CAK will take to Gimme_E
		evt_COOK will take to Gimme_I
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_Gimme_K(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_Gimme_K(work);
		}
		return; } 
		/* State Control Code Ended */

		if (_lfsm.getCurrentState().getId().equals("Gimme_O")){
		/* Control Code for state Gimme_O */
		/* Possible transitions to take at this point are: 
		evt_O will take to Gimme_K
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_Gimme_O(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_Gimme_O(work);
		}
		return; } 
		/* State Control Code Ended */

		if (_lfsm.getCurrentState().getId().equals("Gimme_E")){
		/* Control Code for state Gimme_E */
		/* Possible transitions to take at this point are: 
		evt_Done will take to CheckDessert
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_Gimme_E(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_Gimme_E(work);
		}
		return; } 
		/* State Control Code Ended */

		if (_lfsm.getCurrentState().getId().equals("Gimme_I")){
		/* Control Code for state Gimme_I */
		/* Possible transitions to take at this point are: 
		evt_I will take to Gimme_E
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_Gimme_I(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_Gimme_I(work);
		}
		return; } 
		/* State Control Code Ended */

		if (_lfsm.getCurrentState().getId().equals("CheckDessert")){
		/* Control Code for state CheckDessert */
		/* Possible transitions to take at this point are: 
		evt_Again will take to Gimme_C
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_CheckDessert(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_CheckDessert(work);
		}
		return; } 
		/* State Control Code Ended */

		if (_lfsm.getCurrentState().getId().equals("Init")){
		/* Control Code for state Init */
		/* Possible transitions to take at this point are: 
		evt_init will take to Gimme_C
		*/
		if(work.suscessful()){
		_lfsm.fireFSMEvent(decissionControlFrom_Init(work));
		} else /* work produced fatal errors or exceptions */ {
			errorControlFor_Init(work);
		}
		return; } 
		/* State Control Code Ended */

	throw new Exception("Unexpected State");
	} //end of control method

	private String decissionControlFrom_Initial(StateWork work) {
		/* Possibe target states from this state: 
			| start | 
			 will result in jumping to state: Init
			Do event selection logic here */
	return "start";
	}
	private String decissionControlFrom_Gimme_C(StateWork work) {
		/* Possibe target states from this state: 
			| evt_C | 
			 will result in jumping to state: Gimme_A_or_O
			Do event selection logic here */
	return "evt_C";
	}
	 private String decissionControlFrom_Gimme_A_or_O(StateWork work) {
		/* Possibe target states from this state: 
			| evt_O | 
			 will result in jumping to state: Gimme_O
			| evt_A | 
			 will result in jumping to state: Gimme_K
			Do event selection logic here */
            String produced=((String)work.getWorkOutputs().get("PRODUCED"));
            if (produced.endsWith("O"))
                return "evt_O";
            if (produced.endsWith("A"))
                return "evt_A";
	return "ERROR";
	}
	private String decissionControlFrom_Gimme_K(StateWork work) {
		/* Possibe target states from this state: 
			| evt_CAK | 
			 will result in jumping to state: Gimme_E
			| evt_COOK | 
			 will result in jumping to state: Gimme_I
			Do event selection logic here */
	String produced=((String)work.getWorkOutputs().get("PRODUCED"));
            if (produced.endsWith("OK"))
                return "evt_COOK";
            if (produced.endsWith("AK"))
                return "evt_CAK";
	return "ERROR";
	}
        
	private String decissionControlFrom_Gimme_O(StateWork work) {
		/* Possibe target states from this state: 
			| evt_O | 
			 will result in jumping to state: Gimme_K
			Do event selection logic here */
	return "evt_O";
	}
	private String decissionControlFrom_Gimme_E(StateWork work) {
		/* Possibe target states from this state: 
			| evt_Done | 
			 will result in jumping to state: CheckDessert
			Do event selection logic here */
	return "evt_Done";
	}
	private String decissionControlFrom_Gimme_I(StateWork work) {
		/* Possibe target states from this state: 
			| evt_I | 
			 will result in jumping to state: Gimme_E
			Do event selection logic here */
	return "evt_I";
	}
	private String decissionControlFrom_CheckDessert(StateWork work) {
		/* Possibe target states from this state: 
			| evt_Again | 
			 will result in jumping to state: Gimme_C
			Do event selection logic here */
	return "evt_Again";
	}
	private String decissionControlFrom_Init(StateWork work) {
		/* Possibe target states from this state: 
			| evt_init | 
			 will result in jumping to state: Gimme_C
			Do event selection logic here */
	return "evt_init";
	}
        
        
       
        
	private void errorControlFor_Initial(StateWork work) {
		unifiedErrorControl(work);
	}
	private void errorControlFor_Gimme_C(StateWork work) {
		unifiedErrorControl(work);
	}
	private void errorControlFor_Gimme_A_or_O(StateWork work) {
		unifiedErrorControl(work);
	}
	private void errorControlFor_Gimme_K(StateWork work) {
		unifiedErrorControl(work);
	}
	private void errorControlFor_Gimme_O(StateWork work) {
		unifiedErrorControl(work);
	}
	private void errorControlFor_Gimme_E(StateWork work) {
		unifiedErrorControl(work);
	}
	private void errorControlFor_Gimme_I(StateWork work) {
		unifiedErrorControl(work);
	}
	private void errorControlFor_CheckDessert(StateWork work) {
		unifiedErrorControl(work);
	}
	private void errorControlFor_Init(StateWork work) {
		unifiedErrorControl(work);
	}
} //end of class 
